package com.intgrt.manager.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class ProjectTools {

	public static boolean isNotEmpty(String str){
		if("".equals(str)) 
			return false;
		if("null".equals(str))
			return false;
		if("-1".equals(str))
			return false;
		if(null == str)
			return false;
		return true;
	}
	
	public static boolean isEmpty(String str){
		if("".equals(str)) 
			return true;
		if("null".equals(str))
			return true;
		if("-1".equals(str))
			return true;
		if(null == str)
			return true;
		return false;
	}
	
	public static String curTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(new Date());
	}
	
	public static Date addDay(Date inDate, int day){
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(inDate);
		calendar.add(5, day);
		return calendar.getTime();
	}
	
	public static Date addMonth (Date date,int month){
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		return calendar.getTime();
	}
	
	/**
	 * MD5加密（封装jdk自带的md5加密方法）
	 */
	public static String md5Encode(String source) {
		byte[] oneByte = source.getBytes();
		try {
			oneByte = MessageDigest.getInstance("MD5").digest(oneByte);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		StringBuffer buffer = new StringBuffer(oneByte.length * 2);
        for (int i = 0; i < oneByte.length; i++) {
            if (((int) oneByte[i] & 0xff) < 0x10)
                buffer.append("0");
            buffer.append(Long.toString((int) oneByte[i] & 0xff, 16));
        }
        return buffer.toString();
    }
	
	public static List<Map<String,Object>> getTreeList(List<Map<String,Object>> reslst, String textField){
		if(reslst.size()>0){
			for(Map<String,Object> resmap:reslst){
				resmap.put("state", "closed");
				resmap.put("text", String.valueOf(resmap.get(textField)));
			}
		}
		return reslst;
	}
	
	public static String buildRandomNum(int length) {
		String base = "0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}
	/**
	 * 时间数字类型换成日期格式字符串
	 * 
	 * @param date
	 *            格式"yyyyMMddHHmmss"
	 * @return
	 */
	public static String timeInt2Date2(String date) {
		if (date.length() < 14)
			return "";
		String result = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8) + " "
				+ date.substring(8, 10) + ":" + date.substring(10, 12) + ":" + date.substring(12, 14);
		return result;
	}
	public static String int2DateStr(String date){
		if(date.length() < 8)
			return "0";
		return date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8);
	}

	public static String myformatterDate(String date) {
		if(isNotEmpty(date)){
			if(date.length() <10)
				return null;
			String data[] = date.split("-");
			return data[0]+data[1]+data[2]+"000000";
		}
		return null;
	}
	
}
